package com.finalhomework.dms.controller;

import com.finalhomework.dms.dao.RepairRepository;
import com.finalhomework.dms.dao.StudentRepository;
import com.finalhomework.dms.entities.Repair;
import com.finalhomework.dms.entities.Students;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class RepairController {

    @Autowired
    StudentRepository studentRepository;
    @Autowired
    RepairRepository repairRepository;

    @RequestMapping("/repair")
    public String repairAdd(HttpServletRequest request, Model model){
        Object loginUser=request.getSession().getAttribute("loginUser");
        Students students = studentRepository.getStudentsByNumber(loginUser);
        List<Repair> repAll = repairRepository.findAll();
        for (int i = 0; i < repAll.size(); i++) {
            if (!repAll.get(i).getUserName().equals(students.getName())) {
                repAll.remove(i);
                i--;
            }
        }
        Repair repair = new Repair();
        model.addAttribute("repair",repair);
        model.addAttribute("rep",repAll);
        model.addAttribute("currentStu",students);
        return "repair/repair";
    }

    @PostMapping("/repair/save")
    public String repairSave(Repair repair){
        repair.setState("未处理");
        repairRepository.save(repair);
        return "redirect:/repair";

    }
    @RequestMapping("/pro_repair")
    public String pro_repair(Model model){
        List<Repair> repAll = repairRepository.findAll();
        List<Repair> rep_acc=new ArrayList<>();
        for (int i = 0; i < repAll.size(); i++) {
            if (repAll.get(i).getState().equals("已处理")) {
                rep_acc.add(repAll.get(i));
                repAll.remove(i);
                i--;
            }
        }
        model.addAttribute("rep_acc",rep_acc);
        model.addAttribute("rep",repAll);
        return "repair/pro_repair";

    }

    @GetMapping("/pro_rep/{id}")
    public String pro_rep(@PathVariable("id") Integer id,Model model){
        Repair repairById = repairRepository.getRepairByRid(id);
        repairById.setState("正在处理");
        repairRepository.save(repairById);
        return "redirect:/pro_repair";

    }
    @GetMapping("/accrep/{rid}")
    public String acc_rep(@PathVariable("rid") Integer id){
        Repair byId = repairRepository.getRepairByRid(id);
        byId.setState("已处理");
        repairRepository.save(byId);
        return "redirect:/pro_repair";
    }

    @GetMapping("/delrep/{id}")
    public  String del_rep(@PathVariable("id") Integer id){
        repairRepository.deleteById(id);
        return "redirect:/repair";
    }

    @GetMapping("/reped/{id}")
    public  String rep_ed(@PathVariable("id") Integer id){
        repairRepository.deleteById(id);
        return "redirect:/pro_repair";
    }
}
